
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.*;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import lejos.pc.comm.*;

import java.io.*;

/**
 * A GUI that makes it possible to establish a Bluetooth connection
 * to an NXT controlled car. The car is then controlled by a power
 * and minMotorPowerValueation value sent to the car. The tacho counter value is
 * received and displayed after each command to the car has been performed.
 * 
 * @author Ole Caprani 24.2.12
 *
 * 
 */
public class PCEmbodied extends JFrame implements ActionListener
{
   private TextField nameField = new TextField(12);
   private TextField addressField = new TextField(20);

   private String name = "Desire"; 
   private String address = "00165308d166";
   
   private TextField maxMotorPower = new TextField(10);
   private TextField minMotorPower = new TextField(10);
   
   private JLabel dynamicMax = new JLabel();
   private JLabel dynamicMin = new JLabel();
     
   private NXTComm nxtComm;
   private NXTInfo nxtInfo;
   private InputStream is;
   private OutputStream os;
   private DataInputStream dis;
   private DataOutputStream dos;
   
   private JButton connectButton = new JButton("Connect");   
   private JButton goButton = new JButton("Go");
   private JButton refreshButton = new JButton("Refresh");
   private JButton stopButton = new JButton("Emergency stop");
   private JRadioButton seekInput = new JRadioButton("Seek input");
   private JRadioButton avoidInput= new JRadioButton("Avoid input");


   /**
    * Constructor builds GUI
    */
   public PCEmbodied() 
   {		
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("Control NXT");
      setSize(500,300);
      
      // holds labels and text fields
      JPanel p1 = new JPanel();  
      p1.add(new JLabel("Name:"));
      p1.add(nameField);
      nameField.setText(name);
      p1.add(new JLabel("Address:"));
      p1.add(addressField);
      addressField.setText(address);
     
      try
      {
         nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      }
      catch (NXTCommException nce) {
      }
      nxtInfo = new NXTInfo();
     
      // holds connect button
      JPanel p2 = new JPanel();
      p2.add(connectButton);
      connectButton.addActionListener(this);
      p2.add(goButton);
      goButton.addActionListener(this);
      
      // holds labels and text fields
      JPanel p3 = new JPanel();  
      p3.add(new JLabel("Maximum Motor Power:"));
      p3.add(maxMotorPower);
      maxMotorPower.setText("100");
      p3.add(new JLabel("Minimum Motor Power:"));
      p3.add(minMotorPower);
      minMotorPower.setText("-100");
      
      ButtonGroup seekSettings = new ButtonGroup();
      seekSettings.add(seekInput);
      seekSettings.add(avoidInput);
      avoidInput.setSelected(true);
      
      JPanel p4 = new JPanel();      
      p4.add(seekInput);
      p4.add(avoidInput);

      
      JPanel p5 = new JPanel();
      p5.add(new JLabel("Dynamically maximum set:"));
      dynamicMax.setText("0");
      p5.add(dynamicMax);
      
      JPanel p6 = new JPanel();
      p6.add(new JLabel("Dynamically minimum set:"));
      dynamicMin.setText("0");
      p6.add(dynamicMin);
      
      JPanel p7 = new JPanel();
      p7.add(refreshButton);
      refreshButton.addActionListener(this);
      p7.add(stopButton);
      stopButton.addActionListener(this);
      
      // North area of the frame
      JPanel panel = new JPanel();  
      panel.setLayout(new GridLayout(8,1));
      panel.add(p1);
      panel.add(p2);
      panel.add(p3);
      panel.add(p4);
      panel.add(p5);
      panel.add(p6);
      panel.add(p7);
      add(panel,BorderLayout.NORTH);

   }
   /**
    * Required by action listener; 
    * only action is generated by the get Length button
    */	
   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource()== connectButton)
      {
         String name = nameField.getText();
         String address = addressField.getText();
         nxtInfo.name = name;
         nxtInfo.deviceAddress = address;
         try
         {
            nxtComm.open(nxtInfo);
            is = nxtComm.getInputStream();
            os = nxtComm.getOutputStream();
      	    dis = new DataInputStream(is);
      	    dos = new DataOutputStream(os);
         }
         catch (Exception ex) {
         }
      }	  
	   
      else if(e.getSource()== goButton)
      {
    	 try
         {
    		 
    		String minMotorPowerString = minMotorPower.getText();
     	    int minMotorPowerValue = new Integer(minMotorPowerString).intValue();        
            dos.writeInt(minMotorPowerValue);
            dos.flush();
    		 
    	    String maxMotorPowerString = maxMotorPower.getText();
    	    int maxMotorPowerValue = new Integer(maxMotorPowerString).intValue();        
            dos.writeInt(maxMotorPowerValue);
            dos.flush();
            
            dos.writeBoolean(seekInput.isSelected());
            dos.flush();

            dynamicMin.setText(" "+dis.readInt());
            dynamicMax.setText(" "+dis.readInt());
         }
         catch (Exception ex) {
         }           
      }
      else if(e.getSource()== refreshButton)
      {
    	  try
          {
    		 int min = dis.readInt();
    		 int max = dis.readInt();
             dynamicMin.setText(Integer.toString(min));
             dynamicMax.setText(Integer.toString(max));
          }
          catch (Exception ex) {
          }  
      }
      else if(e.getSource()== stopButton)
      {
    	  try
          {        
             dos.writeInt(0);      
             dos.writeInt(0);
             dos.writeBoolean(seekInput.isSelected());
             dos.flush();
          }
          catch (Exception ex) {
          }    
      }
   }
   
   /**
    * Initialize the display Frame
    */		
   public static void main(String[] args)
   {
      PCEmbodied frame = new PCEmbodied();
      frame.setVisible(true);
   }
}	